﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net;

namespace MyWcfRestClient
{
    public class HttpHelper
    {
        virtual public string UserAgent
        {
            get;
            set;
        }

        public WebResponse PostBinaryData(string requestUrl, byte[] buffer, int lengthToWrite)
        {
            if (string.IsNullOrWhiteSpace(requestUrl))
            {
                throw new ArgumentNullException("RequestURL should be valid URL.");
            }

            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(requestUrl);
            webrequest.Method = "POST";
            webrequest.UserAgent = UserAgent;
            webrequest.ContentType = "application/x-www-form-urlencoded";
            webrequest.AllowAutoRedirect = false;
            Stream requestStream = null;
            try
            {
                requestStream = webrequest.GetRequestStream();
                requestStream.Write(buffer, 0, lengthToWrite);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }

            try
            {
                return webrequest.GetResponse();
            }
            catch (WebException ex)
            {
                throw ex;
            }
        }

        public HttpWebResponse GetRequest(string requestUrl)
        {
            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(requestUrl);
            webrequest.Method = "GET";
            webrequest.UserAgent = UserAgent;
            webrequest.AllowAutoRedirect = false;
            Stream requestStream = null;
            try
            {
                requestStream = webrequest.GetRequestStream();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }

            try
            {
                return (HttpWebResponse)webrequest.GetResponse();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public HttpWebResponse PostTextData(string requestUrl, string postData, bool willForceJson = false)
        {
            if (string.IsNullOrWhiteSpace(requestUrl))
            {
                throw new ArgumentNullException("RequestURL should be valid URL.");
            }

            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(requestUrl);
            webrequest.Method = "POST";
            webrequest.UserAgent = UserAgent;
            if (willForceJson)
            {
                // for WCF interface contract "RequestFormat = WebMessageFormat.Json"
                webrequest.ContentType = "text/json";
            }
            else
            {
                webrequest.ContentType = "text/plain";
            }
            webrequest.AllowAutoRedirect = false;

            Stream requestStream = null;
            try
            {
                requestStream = webrequest.GetRequestStream();
                using (StreamWriter writer = new StreamWriter(requestStream))
                {
                    writer.Write(postData);
                    writer.Close();
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }

            try
            {
                return (HttpWebResponse)webrequest.GetResponse();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public string GetResponseContentString(WebResponse response)
        {
            var stream = response.GetResponseStream();
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse, System.Text.Encoding.UTF8);

            char[] readBuff = new char[response.ContentLength];
            int count = streamRead.Read(readBuff, 0, (int)response.ContentLength);
            return new string(readBuff);
        }
    }

}
